package com.prj.ufdm.demo.tutorial.es;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ElasticSearchClient {

	private static TransportClient transClient;
	
	private static final String ES_NODE_1 = "192.168.128.128";
	private static final String ES_NODE_2 = "192.168.128.129";
	
//	private static final String ES_NODE_1 = "192.168.72.129";
//	private static final String ES_NODE_2 = "192.168.72.130";
	
	public static void main(String[] args) throws Exception {

		ElasticSearchClient esclient = new ElasticSearchClient();
		transClient = esclient.createClient();
		
		esclient.createIndex(transClient, ElasticMappingBuilder.INDEX_RES_LIB,ElasticMappingBuilder.dwxxBuild());
		
//		System.out.println(esclient.searchDocument());

		if(transClient!=null) transClient.close();
	}
	
	
	
	/**
	 * 创建连接
	 * @return
	 * @throws Exception
	 */
	public TransportClient createClient() throws Exception {
		Settings settings = Settings.builder()
		        .put("cluster.name", "my-es-app")
		        .build();
		return new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName(""), 9300))
		        .addTransportAddress(new TransportAddress(InetAddress.getByName(ES_NODE_2), 9300));
	}
	
	/**
	 * 创建索引
	 * @return
	 * @throws Exception
	 */
	public void createIndex(TransportClient tClient,String indexName,XContentBuilder builder) throws Exception {
		IndicesExistsResponse exist = tClient.admin().indices().prepareExists(indexName).get();
		if(exist.isExists()) {
			tClient.admin().indices().prepareDelete(indexName).get();
		}
		tClient.admin().indices().prepareCreate(indexName)
		.addMapping(ElasticMappingBuilder.INDEX_TYPE_DWXX, builder)
//		.addMapping(ElasticMappingBuilder.INDEX_TYPE_DWXX, "dwzh", "type=keyword", "dwmc", "type=text") 
        .setSettings(Settings.builder()
                .put("index.number_of_shards", 4)
                .put("index.number_of_replicas", 1)
        ).get();
	}
	
	/**
	 * 删除索引
	 * @return
	 * @throws Exception
	 */
	public void deleteIndex(TransportClient tClient,String indexName) throws Exception {
		IndicesExistsResponse exist = tClient.admin().indices().prepareExists(indexName).get();
		if(exist.isExists()) {
			tClient.admin().indices().prepareDelete(indexName).get();
		}
	}
	
	/**
	 * 创建索引文件
	 * @return
	 * @throws Exception
	 */
	public void createIndexDocement(TransportClient tClient,String indexName,String type,Map<String,Object> map) throws Exception {
		IndexResponse response = tClient.prepareIndex(indexName, type)
		        .setSource(map)
		        .get();
	}
	
	public List<String> searchDocument(){
		
		List<String> listStr = new ArrayList<String>();
		//SearchRequestBuilder srb1 = transClient.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
		SearchRequestBuilder srb2 = transClient.prepareSearch().setQuery(QueryBuilders.matchQuery("dwzh", "1010020100215")).setSize(1);

		MultiSearchResponse sr = transClient.prepareMultiSearch()
			        .add(srb2)
			        .get();
		for (MultiSearchResponse.Item item : sr.getResponses()) {
		    SearchResponse searchResponse = item.getResponse();
		    SearchHit[] arrSearchHit = searchResponse.getHits().getHits();
		    for(int i=0;i<arrSearchHit.length;i++) {
		    	listStr.add(arrSearchHit[i].getSourceAsString());
		    }
		}
		return listStr;
	}

}
